package com.controller;

import com.dao.RedPacketDao;
import com.pojo.RedPacket;
import com.service.UserRedPacketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/red_packet")
public class RedPacketController {

    @Autowired
    UserRedPacketService userRedPacketService;

    @RequestMapping(value = "/grab", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> grabRedPacket(Long redPacketId, Long userId) {
        int result = userRedPacketService.grabRedPacket(redPacketId, userId);
        Map<String, Object> map = new HashMap<>();
        boolean flag = result > 0;
        map.put("success", flag);
        map.put("message", flag ? "抢红包成功" : "抢红包失败");
        return map;
    }

    //用于测试Spring MVC 配置，返回hello.jsp,显示name 值
    //@RequestMapping("/hello")
    public ModelAndView hello0() {
        ModelAndView view = new ModelAndView();
        view.addObject("name", "张三");
        view.setViewName("hello");
        return view;
    }

    @RequestMapping("/hello")
    public void hello() {
        System.out.println("........");
        userRedPacketService.grabRedPacket(0l, 0l);
    }

}
